package wan.android.photo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {
    private android.widget.GridView gv_all;
    private ArrayList<PhotoGridItem> photoGridItems;
    private PhotoAdapter photoAdapter;
    private LinearLayout layout_selected;
    private HashMap<Integer, View> selected;//存放已经选中的图片(在下面显示)，因为选中和取消选中很频繁，对数组的索引改变多，hashmap优于arrarlist
    private Button btn_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.gv_all = (GridView) findViewById(R.id.gv_all);
        btn_send = (Button) findViewById(R.id.btn_send);
        this.layout_selected = (LinearLayout) findViewById(R.id.layout_selected);
        getAllPic();
        selected = new HashMap<Integer, View>();
        photoAdapter = new PhotoAdapter(this, photoGridItems);
        gv_all.setAdapter(photoAdapter);
        gv_all.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                photoGridItems.get(position).setIsSelectedd(!photoGridItems.get(position).isSelectedd());
                photoAdapter.notifyDataSetChanged();
                PhotoGridItem photoGridItem = photoGridItems.get(position);
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setLayoutParams(new AbsListView.LayoutParams(100, 100));
                if (photoGridItem.isSelectedd()) {
                    //选中了一个图片呢，把该图片添加在下面的选中列表中
                    ImageLoader.getInstance().displayImage("file:///" + photoGridItem.getPath(), imageView);
                    imageView.setTag(view);
                    selected.put(position, imageView);
                    layout_selected.addView(imageView);
                } else {
                    //从选中列表中移除点击的这个图片
                    layout_selected.removeView(selected.get(position));
                    selected.remove(position);
                }
                btn_send.setText(selected.size() + "/" + photoGridItems.size());
            }
        });
        btn_send.setText("0/" + photoGridItems.size());
    }

    private void getAllPic() {
        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

        if (cursor != null) {
            photoGridItems = new ArrayList<PhotoGridItem>();
            while (cursor.moveToNext()) {
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
                PhotoGridItem item = new PhotoGridItem();
                item.setPath(path);
                item.setIsSelectedd(false);
                PhotoGridItem item1 = new PhotoGridItem();
                item1.setPath(path);
                item1.setIsSelectedd(false);
                PhotoGridItem item2 = new PhotoGridItem();
                item2.setPath(path);
                item2.setIsSelectedd(false);
                photoGridItems.add(item);
                photoGridItems.add(item1);
                photoGridItems.add(item2);
            }
            cursor.close();
        }
    }
}
